package com.example.back.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.back.dto.req.HospitalEditReq;
import com.example.back.dto.resp.HospitalResp;
import com.example.back.feign.FileFeign;
import com.example.back.jpa.HospitalRepository;
import com.example.back.mapper.HospitalMapper;
import com.example.back.service.HospitalService;
import com.example.common.entity.Hospital;
import com.example.common.support.ApiException;
import com.example.common.support.Result;
import com.example.common.utils.CheckUtils;
import com.example.common.utils.IdUtils;

import org.javers.spring.annotation.JaversAuditable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author wxy
 * @Date 2021/1/14 13:42
 * @Version 1.0
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalMapper hospitalMapper;
    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private FileFeign fileFeign;

    /**
     * 医院信息
     */
    @Override
    public HospitalResp Info() {
        //HospitalResp hospitalResp = hospitalMapper.findByInfo();
        //hospitalResp.setAddress(hospitalResp.getAddress());
        return hospitalMapper.findByInfo();
    }

    /**
     * 上传文件
     */
    @Override
    public Object UpLoad(MultipartFile file) {
        CheckUtils.checkNull(file, new ApiException(1, "文件不能为空"));
        Result<Object> result = fileFeign.updateFile(file);
        return result.getData();
    }

    /**
     * 更新 医院信息
     */
    @Override
    @JaversAuditable
    public Object UpdateInfo(HospitalEditReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));

        List<Hospital> hospitals = hospitalRepository.findAll();
        if(hospitals.size() != 0){

            Hospital obj = hospitalRepository.findById(req.getID()).orElse(null);
            if(obj==null){
                throw new ApiException(1, "错误的请求参数");
            }

            obj.setName(req.getName());
            obj.setLevel(req.getLevel());
            obj.setBanner(req.getBanner());
            obj.setAddress(req.getAddress());
            obj.setNetDiagnosis(req.getNetDiagnosis());
            obj.setLicenseUrl(req.getLicenseURL());
            obj.setAwarded(req.getAwarded());
            obj.setTel(req.getTel());
            obj.setBadge(req.getBadge());
            obj.setIsPlantOrder(req.getIsPlantOrder());

            return hospitalRepository.save(obj);
        }else{

            Hospital hospital = new Hospital();
            hospital.setId(IdUtils.generateId());
            hospital.setName(req.getName());
            hospital.setLevel(req.getLevel());
            hospital.setBanner(req.getBanner());
            hospital.setAddress(req.getAddress());
            hospital.setNetDiagnosis(req.getNetDiagnosis());
            hospital.setLicenseUrl(req.getLicenseURL());
            hospital.setAwarded(req.getAwarded());
            hospital.setTel(req.getTel());
            hospital.setBadge(req.getBadge());
            hospital.setIsPlantOrder(req.getIsPlantOrder());

            return hospitalRepository.save(hospital);
        }

    }
}
